<!DOCTYPE HTML>
<?php 
require_once'config.php';
require_once'functions.php';
$arrayAnswer = "";
$attempt = 1;
$testID = 1;
	if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
	$num = $_POST['num'];
	$currentQuestion = $num;
	$selectedAnswer = $_POST['questionOptions']; 
	$_SESSION['score'] = $_POST['score']; 
	$score = $_SESSION['score'] ;
if($currentQuestion == 11)
{
$finishTest = finishTest($studentID, $attempt, $currentQuestion, $testID);
}
	}

$getQuestion = getQuestion($currentQuestion);
$num = $_POST['num'];
$putStudentResponse = putStudentResponse($studentID, $attempt, $currentQuestion, $testID);

	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT isKey1 , isKey2 , isKey3 , isKey4 FROM question WHERE questionID='$currentQuestion'";
	$result = mysqli_query($con,$query) or die ("Could not execute getQuestion query");
	$row = mysqli_fetch_assoc($result);
	$arrlength = count($row);
	foreach($row as $answer)
    if ($answer != "0")
   {
	$questionAnswers[] = $answer;
	foreach($questionAnswers as $answer)
{
	$feedbackAnswer = implode(",", $questionAnswers);
	}
   }
if(empty($_POST['questionOptions']))
{
$_POST['questionOptions'] = null;
}
if ($selectedAnswer == $feedbackAnswer)
{
	$score = ($_POST['score'] + 1);
	$feedbackStatus = "<span class='green'> correct.</span>";
}
else
{
	$score = ($_POST['score']);
	$feedbackStatus = "<span class='red'> incorrect.</span>";
}

if (is_array($selectedAnswer))
{
	rsort($selectedAnswer,2);
	foreach($selectedAnswer as $answer)
	{
	$arrayAnswer = implode(",", $selectedAnswer);
	}	
 }

if (strpos($arrayAnswer, $feedbackAnswer) !== false)
 {
	$arrayFeedbackStatus = "<span class='green'> correct.</span>";
  	$score++;
}
else
{
	$arrayFeedbackStatus = "<span class='red'> incorrect.</span>";
}

?>
<html>
<head>
<title>Form</title>
<link rel="stylesheet" href="styles/styles.css" type="text/css" />
</head>
<body bgcolor="#FFFFFF">
<!--<div class="block">
-->  
<div class="overlay"></div>
<div class="modal">
  <?php if (empty($_POST['questionOptions'])) { 
  echo "emptiness"; ?>
  <form method='post' action="postquiz.php" id="feedback">
  <input type="hidden" name="num" value="<?php echo $num ?>">
      <input type="hidden" name="score" value="<?php echo  $score; ?>">
      <input type="hidden" name="getNext" value="TRUE">
      <input type='submit' id='submit' name='back' value='Get Back' class='btn'></form>
  <?php  } ?>   
  
 <?php if (isset($_POST['questionOptions'])) {  ?>    
  <p>You answered:
<?php  if (is_array($_POST['questionOptions'])) 
{ 
echo $arrayAnswer; } 
else { ?>
<?php echo($_POST['questionOptions']);?>
<?php } ?>
    </p>
    <p>The correct answer was <?php echo $feedbackAnswer ;?>. 
      You were
 <?php  if (is_array($_POST['questionOptions'])) { echo $arrayFeedbackStatus; } else { ?>     
      
       <?php echo $feedbackStatus; ?> </p>
               <?php } ?>

    <?php if ($num < 10) { ?>
    <form method="post" action="postquiz.php">
      <input type="hidden" name="attempt" value="<?php echo $attempt ?>">
      <input type="hidden" name="num" value="<?php echo $num + 1 ?>">
      <input type="hidden" name="score" value="<?php echo $score; ?>">
      <input type="submit" id="submit" name="next" value="Next Question" class="btn">
    </form>
    <?php } ?>
    <?php if ($num > 9) { ?>
    <form method="post" id="finished" action="postquiz.php">
      <input type="hidden" name="attempt" value="<?php echo $attempt ?>">
      <input type="hidden" name="num" value="<?php echo $num + 1?>">
      <input type="hidden" name="score" value="<?php echo $score; ?>">
      <input type="submit" id="getResults" name="getResults" value="Get Results" class="btn">
    </form>
    <?php } ?>
        <?php } ?>


  </div>
<!--</div>
--></body>
</html>